import java.util.ArrayList;
import java.util.LinkedList;
import java.util.ListIterator;

public class TestDemo {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        //背后是双向链表
        LinkedList<Integer> linkedList = new LinkedList<>(arrayList);
        //linkedList
        System.out.println(linkedList);
        System.out.println("==========foreach============");
        //linkedList.add(19);
        for(Integer x:linkedList){
            System.out.print(x+" ");
        }
        System.out.println();
        System.out.println("==========for============");
        for (int i = 0; i < linkedList.size(); i++){
            System.out.print(linkedList.get(i) + " ");
        }
        System.out.println();
        System.out.println("==========迭代(正向遍历)============");
        ListIterator<Integer> it = linkedList.listIterator();
        while(it.hasNext()){
            System.out.print(it.next()+" ");
        }
        System.out.println();
        System.out.println("==========迭代(反向遍历)============");

        ListIterator<Integer> it2 = linkedList.listIterator(linkedList.size());
        while(it2.hasPrevious()){
            System.out.print(it2.previous()+" ");
        }
        System.out.println();
    }
}
